<?php

namespace App\Service;

use App\Model\GameTaskDayComplete;
use App\Model\GameTaskDayList;
use App\Model\GameTaskGrowComplete;
use App\Model\GameTaskGrowList;

class TaskCenterService
{
    private static $game_id = 6;

    // 更新麻将每日任务
    public static function updateMjDayTask($uidArr)
    {
        $daliyTaskOne = 3;
        $daliyTaskTwo = 4;
        $game_id = self::$game_id;
        $start = strtotime(date('Y-m-d') . '00:00:00');
        $end = strtotime(date('Y-m-d') . '23:59:59');
        foreach ($uidArr as $val) {
            // 更新对局任务进度
            $where = [['uid', '=', $val['account']], ['game_id', '=', $game_id], ['day_task_id', '=', $daliyTaskOne], ['update_at', '>=', $start], ['update_at', '<=', $end]];
            self::updateDayRecord($where, $daliyTaskOne, $val['account'], $game_id);
            // 更新胜局任务进度
            if ($val['is_win'] == 1) {
                $where2 = [['uid', '=', $val['account']], ['game_id', '=', $game_id], ['day_task_id', '=', $daliyTaskTwo], ['update_at', '>=', $start], ['update_at', '<=', $end]];
                self::updateDayRecord($where2, $daliyTaskTwo, $val['account'], $game_id);
            }
        }
    }

    // 更新成长任务
    public static function updateMjGrow($uidArr)
    {
        $growTaskOne = 3;
        $growTaskTwo = 4;
        $game_id = self::$game_id;
        foreach ($uidArr as $key => $val) {
            // 更新对局任务进度
            $where = [['uid', '=', $val['account']], ['game_id', '=', $game_id], ['grow_task_id', '=', $growTaskOne]];
            self::updateGrowRecord($where, $growTaskOne, $val['account'], $game_id);
            // 更新胜局任务进度
            if ($val['is_win'] == 1) {
                $where2 = [['uid', '=', $val['account']], ['game_id', '=', $game_id], ['grow_task_id', '=', $growTaskTwo]];
                self::updateGrowRecord($where2, $growTaskTwo, $val['account'], $game_id);
            }
        }

    }


    // 更新每日任务
    public static function updateDay($uidArr, $task_id)
    {
        $game_id = self::$game_id;
        $start = strtotime(date('Y-m-d') . '00:00:00');
        $end = strtotime(date('Y-m-d') . '23:59:59');
        foreach ($uidArr as $uid) {
            $where = [
                'uid' => ['=', $uid],
                'game_id' => ['=', $game_id],
                'day_task_id' => ['=', $task_id],
                'update_at' => ['between', [$start, $end]]];
            self::updateDayRecord($where, $task_id, $uid, $game_id);
        }
    }

    // 更新成长任务
    public static function updateGrow($uidArr, $task_id)
    {
        $game_id = self::$game_id;
        foreach ($uidArr as $uid) {
            $where = [
                'uid' => ['=', $uid],
                'game_id' => ['=', $game_id],
                'grow_task_id' => ['=', $task_id],
            ];
            self::updateGrowRecord($where, $task_id, $uid, $game_id);
        }
    }

    // 更新每日任务记录
    private static function updateDayRecord($where, $task_id, $account)
    {
        $game_id = self::$game_id;
        $res = GameTaskDayComplete::checkRecord($where);
        $task_param = GameTaskDayList::checkRecord($task_id);
        if ($task_param['code'] == 0) {
            $complete_num = $task_param['data']['task_param'];
            $time = time();
            if ($res['code'] !== 0) {
                if ($complete_num == 1) {
                    $data = ['uid' => $account, 'game_id' => $game_id, 'day_task_id' => $task_id, 'complete_num' => 1, 'is_complete' => 1, 'is_get' => 2, 'create_at' => $time, 'update_at' => $time];
                } else {
                    $data = ['uid' => $account, 'game_id' => $game_id, 'day_task_id' => $task_id, 'complete_num' => 1, 'is_complete' => 2, 'is_get' => 2, 'create_at' => $time, 'update_at' => $time];
                }
                GameTaskDayComplete::insertRecord($data);
            } else {
                $now_num = $res['data']['complete_num'] + 1;
                if ($complete_num > $now_num) {
                    GameTaskDayComplete::updateRecord($res['data']['id'], ['complete_num' => $now_num, 'update_at' => $time]);
                } elseif ($complete_num == $now_num) {
                    GameTaskDayComplete::updateRecord($res['data']['id'], ['complete_num' => $now_num, 'is_complete' => 1, 'update_at' => $time]);
                }
            }
        }
    }

    // 更新成长任务记录
    private static function updateGrowRecord($where, $task_id, $account)
    {
        $game_id = self::$game_id;
        $res = GameTaskGrowComplete::checkRecord($where);
        $task_param = GameTaskGrowList::checkRecord($task_id);
        if ($task_param['code'] === 0) {
            $complete_num = $task_param['data']['task_param'];
            $time = time();
            if ($res['code'] !== 0) {
                if ($complete_num == 1) {
                    $data = ['uid' => $account, 'game_id' => $game_id, 'grow_task_id' => $task_id, 'complete_num' => 1, 'is_complete' => 1, 'is_get' => 2, 'create_at' => $time, 'update_at' => $time];
                } else {
                    $data = ['uid' => $account, 'game_id' => $game_id, 'grow_task_id' => $task_id, 'complete_num' => 1, 'is_complete' => 2, 'is_get' => 2, 'create_at' => $time, 'update_at' => $time];
                }
                GameTaskGrowComplete::insertRecord($data);
            } else {
                $now_num = $res['data']['complete_num'] + 1;
                if ($complete_num > $now_num) {
                    GameTaskGrowComplete::updateRecord($res['data']['id'], ['complete_num' => $now_num, 'update_at' => $time, 'is_complete' => 2]);
                } elseif ($complete_num == $now_num) {
                    GameTaskGrowComplete::updateRecord($res['data']['id'], ['complete_num' => $now_num, 'is_complete' => 1, 'update_at' => $time]);
                }
            }
        }
    }
}